package co.yixiang.config.thread;

import co.yixiang.utils.SpringContextHolder;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 自定义线程池工具类，用于获取基于配置的线程池实例。
 */
public class ThreadPoolExecutorUtil {

    /**
     * 获取基于配置的自定义线程池实例。
     *
     * @return 配置好的ThreadPoolExecutor实例
     */
    public static ThreadPoolExecutor getPoll() {
        AsyncTaskProperties properties = SpringContextHolder.getBean(AsyncTaskProperties.class);
        /* 根据异步任务配置参数初始化线程池执行器 */
        return new ThreadPoolExecutor(
                properties.getCorePoolSize(),
                properties.getMaxPoolSize(),
                properties.getKeepAliveSeconds(),
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(properties.getQueueCapacity()),
                new TheadFactoryName()
        );
    }
}
